import requestHandler from "@/utils/requestHandler"

export async function getAreas() {
	let result = await requestHandler('/api/area/getAreas', 'GET');
	if (result === undefined) {
		result = await requestHandler('/api/area/getAreas', 'GET');
	}
	if (result.data) {
		const areas = result.data.data;
		areas.forEach(area => {
			if (61 <= area.id && area.id <= 65) {
				area['district'] = '花地玛堂区';
			} else if (66 <= area.id && area.id <= 67) {
				area['district'] = '圣安多尼堂区';
			} else if (68 <= area.id && area.id <= 70) {
				area['district'] = '大堂区';
			} else if (71 <= area.id && area.id <= 73) {
				area['district'] = '望德堂区';
			} else if (74 <= area.id && area.id <= 76) {
				area['district'] = '风顺堂区';
			} else if (area.id === 77) {
				area['district'] = '嘉模堂区';
			} else {
				area['district'] = '圣方济各堂区';
			}
		});
		return areas;
	}
}

export async function getAreaById(areaId) {
	const areas = await getAreas();
	const result = areas.filter(area => area.id === areaId);
	return result.length === 0 ? [] : result[0];
}

export async function getAreasByUserId(userId) {
	return await requestHandler('/api/area/getAreasByUserId', 'GET', {}, {
		userId
	});
}

export async function grantUserArea(userId, areaId) {
	return await requestHandler('/api/area/grant', 'POST', {
		'Content-Type': 'application/json'
	}, {
		userId,
		areaId
	});
}